package Hander;

import Req.Request;
import View.*;
import View.Add.*;
import View.Updata.*;
import entity.TableDTO;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LenderViewHander implements ActionListener {


    //表名字是监听器的属性
    private StringBuilder tableName=new StringBuilder("Personnel");
    private LeaderView leaderView;

    public String getTableName() {
        return tableName.toString();
    }

    public LenderViewHander(LeaderView leaderView) {
        this.leaderView = leaderView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton =(JButton) e.getSource();
        String text=jButton.getText();
        System.out.println(text);
        if("所有人员信息".equals(text)){
            tableName.setLength(0);
            tableName.append("Personnel");
            leaderView.jLabel1.setVisible(true);
            leaderView.searchText1.setVisible(true);
            leaderView.jLabel2.setVisible(true);
            leaderView.searchText2.setVisible(true);
            leaderView.jLabel3.setVisible(true);
            leaderView.searchText3.setVisible(true);
            if(leaderView.getRole().equals("ProjectLeader")){
                leaderView.updataBtn.setVisible(false);
                leaderView.addBtn.setVisible(false);
                leaderView.deteleBtn.setVisible(false);
            }
            if(leaderView.getRole().equals("DepartmentHead")){
                leaderView.updataBtn.setVisible(true);
                leaderView.addBtn.setVisible(true);
                leaderView.deteleBtn.setVisible(true);
            }
        } else if ("所有部门信息".equals(text)) {
            tableName.setLength(0);

            tableName.append("dept");
            leaderView.jLabel1.setVisible(true);
            leaderView.searchText1.setVisible(true);
            leaderView.jLabel2.setVisible(false);
            leaderView.searchText2.setVisible(false);
            leaderView.jLabel3.setVisible(false);
            leaderView.searchText3.setVisible(false);
            if(leaderView.getRole().equals("ProjectLeader")){
                leaderView.updataBtn.setVisible(false);
                leaderView.addBtn.setVisible(false);
                leaderView.deteleBtn.setVisible(false);
            }
            if(leaderView.getRole().equals("DepartmentHead")){
                leaderView.updataBtn.setVisible(true);
                leaderView.addBtn.setVisible(true);
                leaderView.deteleBtn.setVisible(true);
            }
        } else if ("所有项目信息".equals(text)) {
            tableName.setLength(0);

            tableName.append("proj");
            leaderView.jLabel1.setVisible(false);
            leaderView.searchText1.setVisible(false);
            leaderView.jLabel2.setVisible(true);
            leaderView.searchText2.setVisible(true);
            leaderView.jLabel3.setVisible(false);
            leaderView.searchText3.setVisible(false);

            if(leaderView.getRole().equals("ProjectLeader")){
                leaderView.updataBtn.setVisible(true);
                leaderView.addBtn.setVisible(true);
                leaderView.deteleBtn.setVisible(true);
            }
            if(leaderView.getRole().equals("DepartmentHead")){
                leaderView.updataBtn.setVisible(false);
                leaderView.addBtn.setVisible(false);
                leaderView.deteleBtn.setVisible(false);
            }
        } else if ("项目获奖信息".equals(text)) {
            tableName.setLength(0);

            tableName.append("projectawardform");
            leaderView.jLabel1.setVisible(false);
            leaderView.searchText1.setVisible(false);
            leaderView.jLabel2.setVisible(true);
            leaderView.searchText2.setVisible(true);
            leaderView.jLabel3.setVisible(false);
            leaderView.searchText3.setVisible(false);

            if(leaderView.getRole().equals("ProjectLeader")){
                leaderView.updataBtn.setVisible(true);
                leaderView.addBtn.setVisible(true);
                leaderView.deteleBtn.setVisible(true);
            }
            if(leaderView.getRole().equals("DepartmentHead")){
                leaderView.updataBtn.setVisible(false);
                leaderView.addBtn.setVisible(false);
                leaderView.deteleBtn.setVisible(false);
            }

        } else if ("项目人员信息".equals(text)) {
            tableName.setLength(0);
            tableName.append("projectpersonneltable");
            leaderView.jLabel1.setVisible(false);
            leaderView.searchText1.setVisible(false);
            leaderView.jLabel2.setVisible(true);
            leaderView.searchText2.setVisible(true);
            leaderView.jLabel3.setVisible(false);
            leaderView.searchText3.setVisible(false);

            if(leaderView.getRole().equals("ProjectLeader")){
                leaderView.updataBtn.setVisible(true);
                leaderView.addBtn.setVisible(true);
                leaderView.deteleBtn.setVisible(true);
            }
            if(leaderView.getRole().equals("DepartmentHead")){
                leaderView.updataBtn.setVisible(false);
                leaderView.addBtn.setVisible(false);
                leaderView.deteleBtn.setVisible(false);
            }

        } else if ("科研成果信息".equals(text)) {
            tableName.setLength(0);

            tableName.append("achievement");
            leaderView.jLabel1.setVisible(false);
            leaderView.searchText1.setVisible(false);
            leaderView.jLabel2.setVisible(false);
            leaderView.searchText2.setVisible(false);
            leaderView.jLabel3.setVisible(true);
            leaderView.searchText3.setVisible(true);
            if(leaderView.getRole().equals("ProjectLeader")){
                leaderView.updataBtn.setVisible(true);
                leaderView.addBtn.setVisible(true);
                leaderView.deteleBtn.setVisible(true);
            }
            if(leaderView.getRole().equals("DepartmentHead")){
                leaderView.updataBtn.setVisible(false);
                leaderView.addBtn.setVisible(false);
                leaderView.deteleBtn.setVisible(false);
            }

        } else if ("删除".equals(text)) {
            detele();
        } else if ("添加".equals(text)) {
            add();
        } else if ("修改".equals(text)) {
            updata();
        }
        leaderView.reloadTable(tableName.toString());
        if ("查询".equals(text)) {
            leaderView.searchText1.setText("");
            leaderView.searchText2.setText("");
            leaderView.searchText3.setText("");
        }
    }

    private void updata() {
        String[] selectedStudentIds = leaderView.getSelectedStudentIds();
        String name=tableName.toString();
        int key=Integer.parseInt(selectedStudentIds[0]);
        if(selectedStudentIds.length!=1){
            JOptionPane.showMessageDialog(leaderView,"每次修该仅限一行");
            return;
        }
        if(name.equals("Personnel")){
            new UpdataPersonnelView(leaderView,key);
        } else if (name.equals("proj")) {
            new UpdataProjectView(leaderView,key);
        } else if (name.equals("dept")) {
            new UpdataDeptView(leaderView,key);
        } else if (name.equals("achievement")) {
            new UpdataAchievementView(leaderView,key);
        } else if (name.equals("projectawardform")) {
            new UpdataProjectAwardFormView(leaderView,key);
        } else if (name.equals("projectpersonneltable")) {
            new UpdataProjectPersonnelView(leaderView,key);
        }
    }

    private void add() {
        String tableName=this.tableName.toString();
        if(tableName.equals("Personnel")){
            new AddPersonnelView(leaderView,tableName);
        } else if (tableName.equals("achievement")) {
            new AddAchievementView(leaderView,tableName);
        } else if (tableName.equals("dept")) {
            new AddDeptView(leaderView,tableName);
        } else if (tableName.equals("proj")) {
            new AddProjectView(leaderView,tableName);
        } else if (tableName.equals("projectawardform")) {
            new AddProjectAwardFormView(leaderView,tableName);
        } else if (tableName.equals("projectpersonneltable")) {
            new AddProjectPersonnelView(leaderView,tableName);
        }
    }

    private void detele() {
        String[] selectedStudentIds = leaderView.getSelectedStudentIds();
        if(selectedStudentIds.length==0){
            JOptionPane.showMessageDialog(leaderView,"删除行数为0！！！");
            return;
        }
        int option = JOptionPane.showConfirmDialog(leaderView,"你确认要删除选择的"+selectedStudentIds.length
                +"行吗？","确认删除",JOptionPane.YES_NO_OPTION);
        if (option==JOptionPane.YES_NO_OPTION){
            //执行删除
            PersonnelService personnelService=new PersonnelServiceImpl();
            boolean deleteResult=personnelService.delete(selectedStudentIds,tableName.toString());
            if(deleteResult){
                leaderView.reloadTable(tableName.toString());
            }else {
                JOptionPane.showMessageDialog(leaderView,"删除失败！！");
            }
        }
    }

    //这个方法需要使用表明，而表明的获取只能是从监听里面获得
    public TableDTO gettableDTO() {
        PersonnelService personnelService=new PersonnelServiceImpl();
        Request request =new Request();
        request.setDeptsearchKey(leaderView.searchText1.getText());
        request.setObjcsearchKey(leaderView.searchText2.getText());
        request.setAchievementsearchKey(leaderView.searchText3.getText());
        TableDTO tableDTO = personnelService.retrieveStudents(request,tableName.toString());//这才是核心代码
        return tableDTO;
    }



}
